import 'package:OASys/api/UserApi.dart';
import 'package:OASys/components/FormComponents.dart';
import 'package:OASys/components/scaffoldItem.dart';
import 'package:OASys/entity/UserAdd.dart';
import 'package:OASys/utils/strUtils.dart';
import 'package:OASys/utils/validateUtils.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class UserAddPage extends StatelessWidget {
  final Map arguments;

  const UserAddPage({Key key, this.arguments}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ScaffoldItem('添加用户', MyUserAddPage());
  }
}

class MyUserAddPage extends StatefulWidget {
  @override
  _MyUserAddPageState createState() => _MyUserAddPageState();
}

class _MyUserAddPageState extends State<MyUserAddPage> {
  GlobalKey<FormState> formKey = GlobalKey<FormState>();

  UserAdd info = new UserAdd();
  String confirmPwd;
  void save() {
    var form = formKey.currentState;
    if (form.validate()) {
      form.save();
    }
    print(info.toJson().toString());
    if (info.password == null || confirmPwd == null) return;
    if (StrUtils.equals(info.password, confirmPwd)) {
      UserApi().save(
        info,
        success: (res) {
          showMessage(context, "添加成功");
          Navigator.of(context).pop(true);
        },
        error: (err) {
          print(err.code);
          print(err.message);
          showMessage(context, err.message);
        },
      );
    } else {
      showMessage(context, "两次输入密码不一致");
    }
  }

  // 重置表单
  void reset() {
    var form = formKey.currentState;
    form.reset();
    setState(() {
      info.sex = null;
      info.status = null;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 1500,
      width: 350,
      margin: EdgeInsets.fromLTRB(45, 20, 45, 20),
      child: Card(
        child: Container(
          margin: EdgeInsets.all(25),
          child: Form(
            key: formKey,
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    PrefixContanier("用户名:"),
                    Container(
                      width: 200,
                      child: FormInput(
                        hintText: "请输入用户名",
                        validator: (content) {
                          if (content.length < 6) {
                            return "用户名不得少于6位";
                          }
                        },
                        onSave: (value) {
                          info.username = value;
                        },
                      ),
                    )
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    PrefixContanier("密码:"),
                    FormInput(
                      obscureText: true,
                      hintText: '请输入密码',
                      validator: (content) {
                        if (content.length > 12) {
                          return "密码过长";
                        } else if (content.length < 6) {
                          return "密码过短";
                        }
                      },
                      onSave: (value) {
                        info.password = value;
                      },
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    PrefixContanier("确认密码:"),
                    FormInput(
                      obscureText: true,
                      hintText: '请输入确认密码',
                      validator: (content) {
                        if (content.length > 12) {
                          return "密码过长";
                        } else if (content.length < 6) {
                          return "密码过短";
                        }
                      },
                      onSave: (value) {
                        confirmPwd = value;
                      },
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    PrefixContanier("昵称:"),
                    Container(
                      width: 200,
                      child: FormInput(
                        hintText: "请输入昵称",
                        validator: (content) {
                          if (content.length < 3) {
                            return "昵称不得少于3位";
                          }
                        },
                        onSave: (value) {
                          info.nickname = value;
                        },
                      ),
                    )
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    PrefixContanier("手机号码:"),
                    Container(
                      width: 200,
                      child: FormInput(
                        hintText: "请输入手机号码",
                        validator: (content) {
                          if (ValidateUtils.isNotPhone(content)) {
                            return "请输入正确的手机号码";
                          }
                        },
                        onSave: (value) {
                          info.phone = value;
                        },
                      ),
                    )
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    PrefixContanier("邮箱:"),
                    Container(
                      width: 200,
                      child: FormInput(
                        hintText: "请输入邮箱地址",
                        validator: (content) {
                          if (ValidateUtils.isNotEmail(content)) {
                            return "请输入正确的邮箱地址";
                          }
                        },
                        onSave: (value) {
                          info.email = value;
                        },
                      ),
                    )
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Flexible(
                      flex: 2,
                      child: PrefixContanier('性别：'),
                    ),
                    Flexible(
                      flex: 1,
                      child: RadioListTile(
                        title: Text('男'),
                        value: 1,
                        groupValue: info.sex,
                        onChanged: (value) {
                          setState(() {
                            info.sex = value;
                          });
                        },
                      ),
                    ),
                    Flexible(
                      flex: 1,
                      child: RadioListTile(
                        title: Text('女'),
                        value: 2,
                        groupValue: info.sex,
                        onChanged: (value) {
                          setState(() {
                            info.sex = value;
                          });
                        },
                      ),
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    PrefixContanier('状态：'),
                    Text(info.status == 0 ? '启用' : '禁用'),
                    Switch(
                      value: info.status == 0,
                      activeColor: Colors.green,
                      inactiveTrackColor: Colors.red,
                      onChanged: (value) {
                        setState(() {
                          info.status = value ? 0 : 1;
                        });
                      },
                    ),
                  ],
                ),
                SizedBox(
                  height: 30,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    RaisedButton(
                      color: Color.fromRGBO(33, 150, 243, 1),
                      onPressed: save,
                      child: Text(
                        "添 加",
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                    RaisedButton(
                      color: Color.fromRGBO(103, 194, 58, 1),
                      onPressed: reset,
                      child: Text(
                        "重 置",
                        style: TextStyle(color: Colors.white),
                      ),
                    )
                  ],
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
